//
//  WDDefaultSysFunctionManager.m
//  WDPolymerizeSDK
//
//  Created by little People on 2023/6/7.
//

#import "WDDefaultSysFunctionManager.h"
#import "WDNSLog.h"
#import <StoreKit/StoreKit.h>
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
#import "WDCallBack.h"

@implementation WDDefaultSysFunctionManager
{
    NSTimer *vibrateTimer;
}
static WDDefaultSysFunctionManager *_sysFunctionManager = nil;
+ (instancetype)shareSysFunctionManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sysFunctionManager = [[self alloc] init];
        
    });
    return _sysFunctionManager;
}


- (void)sysShareFunctionWithTitle:(NSString *)title url:(NSString *)url
{
    
    NSURL *urlToShare = [NSURL URLWithString:url];
    NSArray *activityItems = @[title, urlToShare];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];

    activityVC.definesPresentationContext = YES;
    //不出现在活动项目
    //activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll];
    UIViewController *rootViewController = [[[UIApplication sharedApplication].windows objectAtIndex:0] rootViewController];
    [rootViewController presentViewController:activityVC animated:YES completion:nil];
    //分享之后的回调
    __weak typeof(activityVC) weekActivity = activityVC;
    activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
        if ([WDCallBack shareCallBack].onShareCallBack)
        {
            [WDCallBack shareCallBack].onShareCallBack(completed);
        }
        if (completed) {
            WDLog(@"completed");
            //分享 成功
        } else {
            WDLog(@"失败");
            //分享 取消
        }
    };
}

- (void)sysScore
{
    if (@available(iOS 10.3, *)) {
        [SKStoreReviewController requestReview];
    }
}

- (void)sysVibrate:(BOOL)repeat
{
//    UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleHeavy];
//
//    [generator impactOccurred];
    AudioServicesPlaySystemSound(1521);
    if (repeat)
    {
        vibrateTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(repeatVibrate:) userInfo:nil repeats:YES];
    }
}

- (void)cancelVibrate
{
    [vibrateTimer invalidate];
    vibrateTimer = nil;
}

- (void)repeatVibrate:(NSTimer *)timer
{
    AudioServicesPlaySystemSound(1521);
}

@end
